<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>popot-2.01: /opt/GoogleCode/popot/trunk/popot-2.01/src/benchmark.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">popot-2.01
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">/opt/GoogleCode/popot/trunk/popot-2.01/src/benchmark.h</div>  </div>
</div>
<div class="contents">
<a href="benchmark_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef POPOT_BENCHMARK</span>
<a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define POPOT_BENCHMARK</span>
<a name="l00003"></a>00003 <span class="preprocessor"></span>
<a name="l00004"></a>00004 <span class="keyword">namespace </span>popot
<a name="l00005"></a>00005 {
<a name="l00006"></a><a class="code" href="namespacepopot_1_1benchmark.html">00006</a>   <span class="keyword">namespace </span>benchmark
<a name="l00007"></a>00007   {
<a name="l00024"></a>00024     <span class="keyword">template</span>&lt;<span class="keyword">typename</span> ALGO, <span class="keyword">typename</span> PROBLEM, <span class="keywordtype">int</span> NB_TRIALS&gt;
<a name="l00025"></a>00025     <span class="keyword">class </span><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html">Benchmark</a>
<a name="l00026"></a>00026     {
<a name="l00027"></a>00027     <span class="keyword">private</span>:
<a name="l00028"></a>00028       <span class="keywordtype">double</span> _mean_error;
<a name="l00029"></a>00029       <span class="keywordtype">double</span> _std_error;
<a name="l00030"></a>00030       <span class="keywordtype">double</span> _mean_nb_f_evaluations;
<a name="l00031"></a>00031       <span class="keywordtype">double</span> _log_progress;
<a name="l00032"></a>00032       <span class="keywordtype">int</span>    _nb_failures;
<a name="l00033"></a>00033       <span class="keywordtype">double</span> * _best_position;
<a name="l00034"></a>00034       <span class="keywordtype">double</span> _best_fitness;
<a name="l00035"></a>00035       <span class="keywordtype">double</span> _success_rate;
<a name="l00036"></a>00036       <span class="keywordtype">int</span> _trial_counter;
<a name="l00037"></a>00037 
<a name="l00038"></a>00038     <span class="keyword">public</span>:
<a name="l00039"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#aeb47c1aa50d6cf0b81b9af5d6c299a46">00039</a>       <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#aeb47c1aa50d6cf0b81b9af5d6c299a46">Benchmark</a>() : _mean_error(0.0),_std_error(0.0), _mean_nb_f_evaluations(0.0),
<a name="l00040"></a>00040         _log_progress(0.0), _nb_failures(0), _best_position(0), _best_fitness(0), _success_rate(0.0), _trial_counter(0)
<a name="l00041"></a>00041       {
<a name="l00042"></a>00042         _best_position = <span class="keyword">new</span> <span class="keywordtype">double</span>[PROBLEM::nb_parameters];
<a name="l00043"></a>00043       }
<a name="l00044"></a>00044 
<a name="l00045"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a99ac51b8dc0c67a3fcef6606a817e671">00045</a>       <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a99ac51b8dc0c67a3fcef6606a817e671">~Benchmark</a>()
<a name="l00046"></a>00046       {
<a name="l00047"></a>00047         <span class="keyword">delete</span>[] _best_position;
<a name="l00048"></a>00048       }
<a name="l00049"></a>00049 
<a name="l00053"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a65c4f52dcffdac93c76dba3f549d87e9">00053</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a65c4f52dcffdac93c76dba3f549d87e9">getMeanError</a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00054"></a>00054 <span class="keyword">      </span>{
<a name="l00055"></a>00055         <span class="keywordflow">return</span> _mean_error;
<a name="l00056"></a>00056       }
<a name="l00057"></a>00057 
<a name="l00061"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a4c5d2b64f54986e3a94fee531ecfa3de">00061</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a4c5d2b64f54986e3a94fee531ecfa3de">getStdError</a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00062"></a>00062 <span class="keyword">      </span>{
<a name="l00063"></a>00063         <span class="keywordflow">return</span> _std_error;
<a name="l00064"></a>00064       }
<a name="l00065"></a>00065 
<a name="l00069"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#ab8a6a9f4ef73441c4d82e1f67eacb4e7">00069</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#ab8a6a9f4ef73441c4d82e1f67eacb4e7">getMeanFEvaluations</a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00070"></a>00070 <span class="keyword">      </span>{
<a name="l00071"></a>00071         <span class="keywordflow">return</span> _mean_nb_f_evaluations;
<a name="l00072"></a>00072       }
<a name="l00073"></a>00073 
<a name="l00078"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a52a907f74d407457913f686bc6975a34">00078</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a52a907f74d407457913f686bc6975a34">getLogProgress</a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00079"></a>00079 <span class="keyword">      </span>{
<a name="l00080"></a>00080         <span class="keywordflow">return</span> _log_progress;
<a name="l00081"></a>00081       } 
<a name="l00082"></a>00082 
<a name="l00086"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#aa37a919f2dff8a32e53a9226da99f3ed">00086</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#aa37a919f2dff8a32e53a9226da99f3ed">getSuccessRate</a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00087"></a>00087 <span class="keyword">      </span>{
<a name="l00088"></a>00088         <span class="keywordflow">return</span> _success_rate;
<a name="l00089"></a>00089       } 
<a name="l00090"></a>00090 
<a name="l00094"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#ad8508d007eda2a781696496f9971e971">00094</a>       <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#ad8508d007eda2a781696496f9971e971">getBestPosition</a>(<span class="keywordtype">double</span> * pos)<span class="keyword"> const</span>
<a name="l00095"></a>00095 <span class="keyword">      </span>{
<a name="l00096"></a>00096         memcpy(pos, _best_position, PROBLEM::nb_parameters*<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));
<a name="l00097"></a>00097       } 
<a name="l00098"></a>00098 
<a name="l00102"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a21a09c7d3d390aac026188bc31126c0f">00102</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a21a09c7d3d390aac026188bc31126c0f">getBestFitness</a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00103"></a>00103 <span class="keyword">      </span>{
<a name="l00104"></a>00104         <span class="keywordflow">return</span> _best_fitness;
<a name="l00105"></a>00105       }
<a name="l00106"></a>00106  
<a name="l00110"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#ad43e98f5c69c7ad0a4b7b052ab42924e">00110</a>       <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#ad43e98f5c69c7ad0a4b7b052ab42924e">run</a>(<span class="keywordtype">int</span> mode=0)
<a name="l00111"></a>00111       {
<a name="l00112"></a>00112         _mean_error = 0.0;
<a name="l00113"></a>00113         _std_error = 0.0;
<a name="l00114"></a>00114         _mean_nb_f_evaluations = 0.0;
<a name="l00115"></a>00115         _log_progress = 0.0;
<a name="l00116"></a>00116         _nb_failures = 0;
<a name="l00117"></a>00117         _best_fitness = 0;
<a name="l00118"></a>00118         _success_rate = 0.0;
<a name="l00119"></a>00119         _trial_counter = 0;
<a name="l00120"></a>00120 
<a name="l00121"></a>00121         ALGO* algo;
<a name="l00122"></a>00122         <span class="comment">// Some temporary variables used to compute online the mean, std, .. </span>
<a name="l00123"></a>00123         <span class="keywordtype">double</span> sum_error = 0;
<a name="l00124"></a>00124         <span class="keywordtype">double</span> sum_square_errors=0;
<a name="l00125"></a>00125         <span class="keywordtype">double</span> sum_fe = 0;
<a name="l00126"></a>00126         <span class="keywordtype">double</span> sum_log = 0;
<a name="l00127"></a>00127         <span class="keywordtype">double</span> init_fitness = 0;
<a name="l00128"></a>00128         
<a name="l00129"></a>00129         <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 1 ; i &lt;= NB_TRIALS ; ++i)
<a name="l00130"></a>00130           {
<a name="l00131"></a>00131             _trial_counter++;
<a name="l00132"></a>00132             PROBLEM::init();
<a name="l00133"></a>00133             
<a name="l00134"></a>00134             algo = <span class="keyword">new</span> ALGO();
<a name="l00135"></a>00135             init_fitness = algo-&gt;getBestFitness();
<a name="l00136"></a>00136             algo-&gt;run();
<a name="l00137"></a>00137 
<a name="l00138"></a>00138             <span class="comment">// Update the statistics</span>
<a name="l00139"></a>00139             sum_error += algo-&gt;getBestFitness();
<a name="l00140"></a>00140             sum_square_errors += algo-&gt;getBestFitness()*algo-&gt;getBestFitness();
<a name="l00141"></a>00141             sum_fe += PROBLEM::count;
<a name="l00142"></a>00142             sum_log += (log(algo-&gt;getBestFitness()) - log(init_fitness))/<span class="keywordtype">double</span>(PROBLEM::count);
<a name="l00143"></a>00143             <span class="keywordflow">if</span>(PROBLEM::has_failed(algo-&gt;getBestFitness()))
<a name="l00144"></a>00144               _nb_failures++;
<a name="l00145"></a>00145             <span class="keywordflow">if</span>(i == 1 || algo-&gt;getBestFitness() &lt; _best_fitness)
<a name="l00146"></a>00146               {
<a name="l00147"></a>00147                 _best_fitness = algo-&gt;getBestFitness();
<a name="l00148"></a>00148                 algo-&gt;getBestPosition(_best_position);
<a name="l00149"></a>00149               }
<a name="l00150"></a>00150               
<a name="l00151"></a>00151             <span class="comment">// Eventually print the statistics</span>
<a name="l00152"></a>00152             <span class="keywordflow">if</span>(mode)
<a name="l00153"></a>00153               {
<a name="l00154"></a>00154                 _mean_error = sum_error/double(i);
<a name="l00155"></a>00155                 _std_error = sqrt((sum_square_errors - 2.0 * _mean_error * sum_error + <span class="keywordtype">double</span>(i) * _mean_error*_mean_error)/<span class="keywordtype">double</span>(i));
<a name="l00156"></a>00156                 _mean_nb_f_evaluations = sum_fe/double(i);
<a name="l00157"></a>00157                 _log_progress = sum_log/double(i);
<a name="l00158"></a>00158                 _success_rate = 100.*double(i-_nb_failures)/double(i);
<a name="l00159"></a>00159 
<a name="l00160"></a>00160                 <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a97982d69125e42fedb9dc1bed592b1d7">print</a>(std::cout);
<a name="l00161"></a>00161               }
<a name="l00162"></a>00162             
<a name="l00163"></a>00163             <span class="comment">// Free the memory</span>
<a name="l00164"></a>00164             PROBLEM::free();
<a name="l00165"></a>00165             <span class="keyword">delete</span> algo;
<a name="l00166"></a>00166           }
<a name="l00167"></a>00167                      
<a name="l00168"></a>00168         <span class="comment">// Before leaving, ensure that the statistics are up-to-date;</span>
<a name="l00169"></a>00169         _mean_error = sum_error/double(NB_TRIALS);
<a name="l00170"></a>00170         _std_error = sqrt((sum_square_errors - 2.0 * _mean_error * sum_error + <span class="keywordtype">double</span>(NB_TRIALS) * _mean_error*_mean_error)/<span class="keywordtype">double</span>(NB_TRIALS));
<a name="l00171"></a>00171         _mean_nb_f_evaluations = sum_fe/double(NB_TRIALS);
<a name="l00172"></a>00172         _log_progress = sum_log/double(NB_TRIALS);
<a name="l00173"></a>00173         _success_rate = 100.*double(NB_TRIALS-_nb_failures)/double(NB_TRIALS);
<a name="l00174"></a>00174       }
<a name="l00175"></a>00175 
<a name="l00179"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a97982d69125e42fedb9dc1bed592b1d7">00179</a>       <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a97982d69125e42fedb9dc1bed592b1d7">print</a>(std::ostream &amp; os)<span class="keyword"> const</span>
<a name="l00180"></a>00180 <span class="keyword">      </span>{
<a name="l00181"></a>00181         os &lt;&lt; <span class="stringliteral">&quot;Trial=&quot;</span> &lt;&lt; _trial_counter &lt;&lt; <span class="stringliteral">&quot;;&quot;</span>;
<a name="l00182"></a>00182         os &lt;&lt; <span class="stringliteral">&quot;Error(mean)= &quot;</span> &lt;&lt; <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a65c4f52dcffdac93c76dba3f549d87e9">getMeanError</a>() &lt;&lt; <span class="stringliteral">&quot;;&quot;</span>;
<a name="l00183"></a>00183         os &lt;&lt; <span class="stringliteral">&quot;Error(std)= &quot;</span> &lt;&lt; <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a4c5d2b64f54986e3a94fee531ecfa3de">getStdError</a>() &lt;&lt; <span class="stringliteral">&quot;;&quot;</span>;
<a name="l00184"></a>00184         os &lt;&lt; <span class="stringliteral">&quot;FE(mean)= &quot;</span> &lt;&lt; <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#ab8a6a9f4ef73441c4d82e1f67eacb4e7">getMeanFEvaluations</a>() &lt;&lt; <span class="stringliteral">&quot;;&quot;</span>;
<a name="l00185"></a>00185         os &lt;&lt; <span class="stringliteral">&quot;Log_progress(mean)= &quot;</span> &lt;&lt; <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a52a907f74d407457913f686bc6975a34">getLogProgress</a>() &lt;&lt; <span class="stringliteral">&quot;;&quot;</span>;
<a name="l00186"></a>00186         os &lt;&lt; <span class="stringliteral">&quot;Best_min= &quot;</span> &lt;&lt; <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a21a09c7d3d390aac026188bc31126c0f">getBestFitness</a>() &lt;&lt; <span class="stringliteral">&quot;;&quot;</span>;
<a name="l00187"></a>00187         os &lt;&lt; <span class="stringliteral">&quot;Success_rate= &quot;</span> &lt;&lt; <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#aa37a919f2dff8a32e53a9226da99f3ed">getSuccessRate</a>() &lt;&lt; <span class="stringliteral">&quot;%;&quot;</span>;
<a name="l00188"></a>00188         os &lt;&lt; std::endl;
<a name="l00189"></a>00189       }
<a name="l00190"></a>00190 
<a name="l00191"></a><a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a062730a0c23b3db549693670b99e7806">00191</a>       <span class="keyword">friend</span> std::ostream &amp; <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a062730a0c23b3db549693670b99e7806">operator &lt;&lt;</a>(std::ostream &amp; os, <span class="keyword">const</span> <a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html">Benchmark</a> &amp;b)
<a name="l00192"></a>00192         {
<a name="l00193"></a>00193           b.<a class="code" href="classpopot_1_1benchmark_1_1Benchmark.html#a97982d69125e42fedb9dc1bed592b1d7">print</a>(os);
<a name="l00194"></a>00194           <span class="keywordflow">return</span> os;
<a name="l00195"></a>00195         }
<a name="l00196"></a>00196     };
<a name="l00197"></a>00197   } <span class="comment">// benchmark</span>
<a name="l00198"></a>00198 } <span class="comment">// popot</span>
<a name="l00199"></a>00199 
<a name="l00200"></a>00200 <span class="preprocessor">#endif</span>
</pre></div></div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.5
</small></address>

</body>
</html>
